package com.jxau.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSONObject;
import com.jxau.entity.Article;
import com.jxau.entity.Responses;
import com.jxau.util.entiy.PhotoData;
import com.jxau.util.entiy.UploadPhoto;
import com.jxau.entity.view.IndexArticles;
import com.jxau.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.github.pagehelper.PageInfo;
import com.jxau.entity.Article;
import com.jxau.entity.Temp;
import com.jxau.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.ArrayList;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @RequestMapping("/findArticle")
    public List<Temp> getArticleById(int id) {
        return articleService.getArticleById(id);
    }

    @RequestMapping("/deleteArticle")
    public int deleteArticle(int id) {
        return articleService.deleteArticle(id);
    }

    @RequestMapping("/page")
    public List<Temp> getPages(int pageNum, int pageSize) {
        List<Temp> articles = articleService.getPages(pageNum, pageSize);
        return articles;
    }

    /**
     * 获取分页条的相关信息
     *
     * @return
     */
    @RequestMapping("/pageData")
    public PageInfo<Temp> getPagesData() {
        return articleService.getPagesData();
    }

//    @GetMapping("/queryAllArticles")
//    public Responses queryAllArticles() {
//        ArrayList<IndexArticles> articles = articleService.queryAllArticles();
//        return Responses.success("查询所有文章成功！（非登录状态）", articles);
//    }

    @GetMapping("/queryAllArticles")
    public Responses queryAllArticlesWithUid(String uid, @RequestParam(defaultValue = "1") int nowPage) {
        ArrayList<IndexArticles> articles = articleService.queryAllArticlesWithUid(uid, nowPage);
        PageInfo<IndexArticles> pageInfo = new PageInfo<>(articles);
        return Responses.success("查询所有文章成功！", articles, pageInfo.getPages());
    }

    @GetMapping("queryFollowUsersArticles")
    public Responses queryFollowUsersArticles(String uid, @RequestParam(defaultValue = "1") int nowPage) {
        ArrayList<IndexArticles> articles = articleService.queryFollowUsersArticles(uid, nowPage);
        PageInfo<IndexArticles> pageInfo = new PageInfo<>(articles);
        return Responses.success("查询关注用户发布的文章成功！", articles, pageInfo.getPages());
    }


    @PostMapping("/addArticle")
    public Responses addArticle(Article article) {
        articleService.addArticle(article);
        return Responses.success("添加文章成功！");
    }

    @GetMapping("/queryArticlesByAuthorId")
    public Responses queryArticlesByAuthorId(String authorId,@RequestParam(defaultValue = "1") int nowPage) {
        ArrayList<Article> articles = articleService.queryArticlesByAuthorId(authorId,nowPage);
        PageInfo<Article> pageInfo = new PageInfo<>(articles);
        return Responses.success("查询用户的文章成功！", articles, pageInfo.getPages());
    }

    @GetMapping("/findArticleByStrWithLogin")
    public Responses findArticleByStrWithLogin(String str, String uid, @RequestParam(defaultValue = "1") int nowPage) {
        ArrayList<IndexArticles> articles = articleService.findArticleByStrWithLogin(str, uid, nowPage);
        PageInfo<IndexArticles> pageInfo = new PageInfo<>(articles);
        return Responses.success("搜索成功！（登录状态）", articles, pageInfo.getPages());
    }

    @GetMapping("/queryMyCollection")
    public Responses queryMyCollection(String uid,@RequestParam(defaultValue = "1") int nowPage) {
        ArrayList<Article> myCollection = articleService.queryMyCollection(uid,nowPage);
        PageInfo<Article> pageInfo = new PageInfo<>(myCollection);
        return Responses.success("查询用户收藏文章成功！", myCollection,pageInfo.getPages());
    }

    @PutMapping("/viewPlus")
    public Responses viewPlus(int articleId, HttpSession session) {
        articleService.viewPlus(articleId,session);
        return Responses.success("浏览量加1成功！");
    }
    @GetMapping("/queryOneArticleWithUid")
    public Responses queryOneArticleWithUid(int articleId,String uid){
        Article article = articleService.queryOneArticleWithUid(articleId,uid);
        return Responses.success("查询单篇文章成功",article);
    }
    @PutMapping("/makeTop")
    public Responses makeTop(int articleId) {
        articleService.makeTop(articleId);
        return Responses.success("置顶成功！");
    }
    @PutMapping("/cancelTop")
    public Responses cancelTop(int articleId) {
        articleService.cancelTop(articleId);
        return Responses.success("取消置顶成功！");
    }
}
